package com.iwinding.ladygg.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.iwinding.ladygg.R;
import com.iwinding.ladygg.net.response.SosRecordRp;
import com.iwinding.ladygg.utils.CommonUtil;
import com.iwinding.ladygg.utils.DateUtil;
import com.iwinding.ladygg.utils.RoundedTransformation;
import com.squareup.picasso.Picasso;

import java.text.ParseException;
import java.util.List;

/**
 * Created by hezhiyong on 2017/3/17.
 */

public class RecordAdapter extends BaseAdapter<SosRecordRp> {

    public RecordAdapter(Context mContext, List mList) {
        super(mContext, mList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.icane_adapter_record, null);
        }
        final ImageView icon = ViewHolder.get(convertView, R.id.icane_record_icon);
        final TextView name = ViewHolder.get(convertView, R.id.icane_record_name);
        final TextView launch = ViewHolder.get(convertView, R.id.icane_launch_time);
        final TextView locale = ViewHolder.get(convertView, R.id.icane_record_locale);
        final Button btn = ViewHolder.get(convertView, R.id.icane_record_button);

        final SosRecordRp rp = mList.get(position);
        if (!TextUtils.isEmpty(rp.headImg)) {
            String url = CommonUtil.utf8Togb2312(rp.headImg);
            Picasso.with(mContext).load(url).transform(new RoundedTransformation(9, 5)).into(icon);
        }
        name.setText(TextUtils.isEmpty(rp.nickName) ? (TextUtils.isEmpty(rp.uname) ? "" : rp.uname) : rp.nickName);
        String time = rp.sendTime;
        try {
            time = DateUtil.getTime(rp.sendTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        launch.setText(TextUtils.isEmpty(time) ? "" : time);
        locale.setText(TextUtils.isEmpty(rp.address) ? "" : rp.address);

        switch (rp.status) {
            case 0:
                btn.setText(R.string.icane_my_to_void);
                btn.setBackgroundResource(R.drawable.red_round_button);
                break;
            case 1:
                btn.setText(R.string.icane_my_complete);
                btn.setBackgroundResource(R.drawable.green_round_button);
                break;
            case 2:
                btn.setText(R.string.icane_my_doing);
                btn.setBackgroundResource(R.drawable.pink_round_button);
                break;
        }
        return convertView;
    }


}
